package testcase;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.NoSuchElementException;

import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.junit.Test;

import satir.SatirTherapy;


import junit.framework.TestCase;

public class In_Lv_5_SatirTherapy_1 extends TestCase {

   
    public static class ScannerCorrector{
	private static ArrayList<String> input;
	private static int index;
	public static ByteArrayInputStream initialByByteArrayInputStream(String input){
	    index = 0;
	    ScannerCorrector.input = new ArrayList<String>();
	    String[] pieces = input.split(" ");
	    for (int i = 0; i < pieces.length; i++) {
		String value = pieces[i].trim();
		if(value.isEmpty())
		    continue;
		ScannerCorrector.input.add(value);
	    }
	    return new ByteArrayInputStream(input.getBytes());
	}
	public static String next(){
	    String value = input.get(index++);
	    System.err.println(value);
	    return value;
	}
    }

    /**
     * Sets up the test fixture. Called before every test case method.
     */
    public void setUp() {

	// give choice
	
    }

    /**
     * Tears down the test fixture. Called after every test case method.
     */
    public void tearDown() {
    }

    /**
     * Step number : 1 Goal : Question asked without choices and use magic tool
     * , so no need to answer, no AskQuestionCmd has not been recorded , the
     * player's self-esteem has not been added
     */
    @Test
    public void testdoActivateCard_1() {

	// Force use magic tool card

	// system out
	final ByteArrayOutputStream systemOut = new ByteArrayOutputStream();
	PrintStream original = System.out;
	System.setOut(new PrintStream(systemOut)); // Start store the output
	String input = "1 2 Torrey Antony T A 1 2 4 3";
	ByteArrayInputStream in = ScannerCorrector.initialByByteArrayInputStream(input);
	System.setIn(in);

	try {
	    SatirTherapy.main(new String[] { "test" }); // Run the method to be
							// tested
	} catch (NoSuchElementException e) {
	}
	System.setOut(original); // Store the output unit here

	// Retrieve the actual result
	String actual = systemOut.toString();

	// Expected result
	String expected = "Hello\r\n" +
			"Please indicate your selection.\r\n" +
			"[1] Play game.\r\n" +
			"[2] Analyze specific user.\r\n" +
			"[3] Exit\r\n" +
			"Input the number of players:(2-6)\r\n" +
			"There are 2 players\r\n" +
			"Player 1 please input your name:\r\n" +
			"Hi [Torrey]\r\n" +
			"Player 2 please input your name:\r\n" +
			"Hi [Antony]\r\n" +
			"Torrey please choose your figure:\r\n" +
			"[Torrey] your figure is T\r\n" +
			"Antony please choose your figure:\r\n" +
			"[Antony] your figure is A\r\n" +
			"Torrey, Please choose the card No.\r\n" +
			"-----\r\n" +
			"|1|2|3|4|\r\n" +
			"---------\r\n" +
			"Card No. Result: Torrey & Role : Father\r\n" +
			"Magic Tool Card : \r\n" +
			"Skip Act Card | Accomplish Event Card | Skip Say Card | Skip Event Card | \r\n" +
			"Antony, Please choose the card No.\r\n" +
			"-----\r\n" +
			"|1|2|\r\n" +
			"-----\r\n" +
			"Card No. Result: Antony & Role : Daughter\r\n" +
			"Magic Tool Card : \r\n" +
			"Skip Any Card | Accomplish Any Card | Accomplish Say Card | Accomplish Act Card | \r\n" +
			"-------------------------------------------------\r\n" +
			"Now it is your turn, Antony\r\n" +
			"Please indicate your selection.\r\n" +
			"[1] Roll Dice\r\n[2] View Board\r\n" +
			"[3] View Status\r\n" +
			"[4] Exit\r\n" +
			"Antony roll a number of 1\r\n\r\n" +
			"Here is the new position\r\n" +
			"***********************************\r\n" +
			"| T   ||A    ||     ||     ||     |\r\n" +
			"|     ||     ||     ||     ||     |\r\n" +
			"|     ||     ||     ||     ||     |\r\n" +
			"|     ||     ||     ||     ||     |\r\n" +
			"|     ||Say  ||Act  ||Event||     |\r\n" +
			"***********************************\r\n\r\n" +
			"*******                     *******\r\n" +
			"|     |                     |     |\r\n" +
			"|     |                     |     |\r\n" +
			"|     |                     |     |\r\n" +
			"|     |                     |     |\r\n" +
			"|     |                     |     |\r\n" +
			"*******                     *******\r\n\r\n" +
			"*******                     *******\r\n" +
			"|     |                     |     |\r\n" +
			"|     |                     |     |\r\n" +
			"|     |                     |     |\r\n" +
			"|     |                     |     |\r\n" +
			"|Say  |                     |     |\r\n" +
			"*******                     *******\r\n\r\n" +
			"*******                     *******\r\n" +
			"|     |                     |     |\r\n" +
			"|     |                     |     |\r\n" +
			"|     |                     |     |\r\n" +
			"|     |                     |     |\r\n" +
			"|Act  |                     |Say  |\r\n" +
			"*******                     *******\r\n\r\n" +
			"***********************************\r\n" +
			"|     ||     ||     ||     ||     |\r\n" +
			"|     ||     ||     ||     ||     |\r\n" +
			"|     ||     ||     ||     ||     |\r\n" +
			"|     ||     ||     ||     ||     |\r\n" +
			"|     ||Event||     ||     ||Act  |\r\n" +
			"***********************************\r\n\r\n" +
			"Say Card 3\r\n" +
			"Please ask 'What did you do today?' to any family member.\r\n\r\n" +
			"Would you like to use Magic Tools? ([1]Yes or [2]No)\r\n" +
			"Use which Magic Tools?\r\n" +
			"1. Skip Any Card\r\n" +
			"2. Accomplish Any Card\r\n" +
			"3. Accomplish Say Card\r\n" +
			"Mission is Skipped\r\n" +
			"-------------------------------------------------\r\n" +
			"Now it is your turn, Torrey\r\n" +
			"Please indicate your selection.\r\n" +
			"[1] Roll Dice\r\n[2] View Board\r\n" +
			"[3] View Status\r\n" +
			"[4] Exit\r\n" +
			"..............................\r\n" +
			"Please indicate your selection.\r\n" +
			"[1] Play game.\r\n" +
			"[2] Analyze specific user.\r\n" +
			"[3] Exit\r\n" +
			"System Now Exit....\r\n";

	assertEquals(expected, actual);
    }


    @Aspect
    public static class TestAspects {

	@Pointcut("call(String java.util.Scanner.next*()) && cflow(execution(* testcase.In_Lv_5_SatirTherapy_1.test*()))")
	public void interceptScanerNext() {
	}

	@Around("interceptScanerNext()")
	public String interceptScanerNextAround(){
		return ScannerCorrector.next();
	}
	
    }
}
